package com.itheima.controller;


import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     * @param checkGroup
     * @return
     */
    @PostMapping("/add")
    public Result  add(@RequestParam List<Integer> checkitemIds, @RequestBody CheckGroup checkGroup){
        log.info("新增检查组：{}", checkGroup);
        checkGroupService.add(checkitemIds,checkGroup);
    return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 分页查询检查组
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询检查组:{}",queryPageBean);
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Result deleteById(Integer id){
        log.info("删除检查组：{}", id);
        checkGroupService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     *  根据根据套餐id查询关联的检查项id
     * @param  setmealId
     * @return
     */
    @GetMapping("findCheckGroupIdsBySetmealId")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam Integer setmealId){
        log.info("根据套餐ID查询检查组ID:{}",setmealId);
        List<Integer> checkGroups = checkGroupService.findCheckGroupIdsBySetmealId(setmealId);
        return new Result(true,"查询检查组ID成功",checkGroups);
    }
    /**
     * 根据ID查询检查组
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.info("根据ID查询检查组:{}",id);
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true,"查询检查组成功",checkGroup);
    }
    /**
     * 查询所有检查项
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有检查项");
        List<CheckGroup> checkGroups =checkGroupService.findAll();
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups);
    }

    /**
     * 编辑检查组
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestParam List<Integer> checkitemIds, @RequestBody CheckGroup checkGroup){
        log.info("编辑检查组：{}", checkGroup);
        checkGroupService.edit(checkitemIds,checkGroup);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }
}
